<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" href="/layui/css/layui.css">
  <script src="/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<form class="layui-form" style="padding-top: 15px;">
  <input name="username" type="text" hidden="hidden" th:value="${user.username}">
  <div class="layui-form-item">
    <label class="layui-form-label">用户角色</label>
    <div class="layui-input-block role-checked" th:each="item:${roles}">
      <input name="role" th:value="${item.roleId}" th:checked="${item.status==1}"
             type="checkbox" th:attr="title=${item.roleCnName}">
      <br>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">状态</label>
    <div class="layui-input-block">
      <input type="radio" name="status" value="1" title="有效" th:checked="${user.status==1}">
      <input type="radio" name="status" value="0" title="无效" th:checked="${user.status!=1}">
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="formUp">立即提交</button>
    </div>
  </div>
</form>

<script>
  //Demo
  layui.use(['form', 'jquery', 'layer'], function () {
    var form = layui.form;
    var $ = layui.jquery
    var layer = layui.layer

    //监听提交
    form.on('submit(formUp)', function (data) {
      //layer.msg(JSON.stringify(data.field));
      var roles = new Array()
      $('.role-checked input[name="role"]:checked').each(function () {
        roles.push($(this).val())
      })
      $.ajax({
        data: {role: roles, status: data.field.status, username: data.field.username},
        url: '/admin/updateInfo',
        type: 'post',
        success(data) {
          if (data.code == 0) {
            layer.msg('修改成功')
          } else {
            layer.msg(data.message)
          }
        }
      })
      return false;
    });
  });
</script>
</body>
</html>
